/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.risk.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.risk.bean.CorpBean;
import com.koron.risk.service.api.CorpManagementService;
import com.koron.risk.util.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "corpManagement", description = "公司管理")
@RestController
@RequestMapping("/corpManagement")
public class CorpManagementAction {

    @Autowired
    private CorpManagementService corpManagementService;

    @ApiOperation(value = "获取公司列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/corpList.htm", method = {RequestMethod.GET})
    public MessageBean<?> getCorpList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        } MessageBean<?> msg = corpManagementService.corpList(page, pageSize, null);
        return msg;
    }


    @ApiOperation(value = "获取片区可选的公司列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/canselectcorp.htm", method = {RequestMethod.GET})
    public MessageBean<?> canSelectCorp(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        MessageBean<?> msg = corpManagementService.corpOutOfDistrict(page, pageSize);
        return msg;
    }

    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "已开通公司列表")
    @RequestMapping(value = "/corpOpenedList.htm", method = {RequestMethod.GET})
    public MessageBean<?> corpOpenedList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        MessageBean<List> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        msg.setCode(0);
        msg.setDescription("SUCCESS");
        // msg.setData(corpManagementService.corpBeanList());
        msg.setData(corpManagementService.queryCorpMsg());
        return msg;
    }


    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "公司业务开通")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "公司名称", required = true, paramType = "query", dataType = "String")})
    @RequestMapping(value = "/openCorp.htm", method = {RequestMethod.POST})
    @Log(title = "公司业务开通", businessModule = "公司管理", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> openCorp(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode, String corpName) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        MessageBean msg = corpManagementService.openCorp(corpCode, corpName, user);
        return msg;
    }

    @ApiOperation(value = "编辑公司信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "files", value = "上次的附件信息" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "description", value = "公司信息描述" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "keppAddress", value = "要保存图片地址，多个之间用，分开" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "delAddress", value = "要删除的图片地址，多个之间用，分开" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "generalManager", value = "总经理code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "generalManagerName", value = "总经理名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "chairman", value = "董事长code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "chairmanName", value = "董事长名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mssGeneralManager", value = "安全部总经理code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mssGeneralManagerName", value = "安全部总经理名称", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/updateCorp.htm", method = {RequestMethod.POST})
    @Log(title = "编辑公司信息", businessModule = "公司管理", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<Integer> update(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore CorpBean corpBean, MultipartFile[] files,
                                       @RequestParam(required = false) final String keppAddress, @RequestParam(required = false) final String delAddress) {
        MessageBean<Integer> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        corpBean.setUpdateUser(user.getName());
        return corpManagementService.update(corpBean, files, keppAddress, delAddress);
    }

    @ApiOperation(value = "查看已开通公司详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code" , paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/details.htm", method = {RequestMethod.POST})
    public MessageBean<CorpBean> details(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam final String corpCode) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return corpManagementService.getOne(corpCode);
    }
}
